package castle.defense.project;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;

/**
 * The Castle class is the implementation of the renderable object for the castle in the game.
 * The top of the castle will be 15% of the screen's height.
 * 
 *  The class defining the player, which is essentially the user.
 * The player has health points that correspond to the "health" of
 * their castle, an amount of spendable credits that may be used to 
 * purchase items from the store, a score that carries through the game,
 * and an inventory of weapons at the player's disposal. Finally, the
 * player can either be of class "ninja" or "pirate," as determined by the
 * classType field.
 * 
 * The player may take damage from enemy attacks, buy an item from the store,
 * switch the currently equipped weapon, and have their equipped weapon attack
 * according to interactions with the touch screen of the android device.
 * 
 * 
 * @author KevinJoslyn + Daniel Wasserman
 *
 */

public class Castle extends Renderable {
	public static double scaleX = 100, scaleY = 15;
	public static int castle = 12040;
	static final int height = 0;
	//15% of the screen will be the castle
	static final double heightP = 15;
	static double top;
	private Paint p;
	private RectF r;

	public int baseHealth;
	public int healthPoints;
	public String classType;
	public int credits;
	public int score;
	public ArrayList<Weapon> inventory;
	public ImmobileWeaponSet curWeapon;

	/**
	 * Basic Player constructor
	 * 
	 * @param height 	The height of the Player class (represented by the castle) 
	 * 					on the game field
	 * 
	 * @param ct		The class type (either "pirate" or "ninja")
	 * @param bh		The Player's base health (determined by the
	 * 					desired handicap)
	 * @param credits	The player's starting amount of credits
	 * @param score		The player's starting score
	 */
	public Castle(String ct, int bh, int credits, int score) {

		//super(height); To be added when MyDrawable is changed
		classType = ct;
		baseHealth = healthPoints = bh;
		this.credits = credits;
		this.score = score;
		inventory = new ArrayList<Weapon>();
		r = PE.regionByPercent(0, 15, 100, 15);
		top = PE.screenPercentY(15);
		p = new Paint();
		p.setStyle(Style.FILL_AND_STROKE);
		p.setColor(Color.GRAY);
		inventory.add(Weapon.createGun());
		curWeapon = ImmobileWeaponSet.createBasicSet();
	}

	/**
	 * Subtracts hit points from the player's health (as a result of 
	 * the player's castle being hit by an enemy attack.) 
	 * 
	 * @param amt	The amount of damage taken		
	 */
	public void takeDamage(int amt) {

		healthPoints -= amt;
	}

	/**
	 * Adds hit points to the player's health (as a result of an
	 * item's effect.)
	 * 
	 * @param amt	The amount of hit points to be added
	 */
	public void repair(int amt) {

		healthPoints += amt;
	}

	/**
	 * Method to implement a purchase of a weapon. Returns false if 
	 * the player does not have enough credits to buy the desired weapon, 
	 * and true otherwise. If the weapon is successfully bought, it is added
	 * to the player's inventory, and the appropriate amount of credits are
	 * subtracted from the player.
	 * 
	 * @param weapon	The weapon to be bought
	 * @return 			Returns false if the player does not have enough 
	 * 					credits to buy the desired weapon, and true otherwise.
	 */
	public boolean buy(Weapon weapon) {

		if(credits < weapon.cost)
			return false;
		credits -= weapon.cost;
		inventory.add(weapon);
		weapon.purchased = true;
		return true;
	}

	/**
	 * Causes a selected weapon to become the current weapon 
	 * equipped by the player.
	 * 
	 * @param w		The weapon to be equipped.
	 */
	public void switchWeapon(ImmobileWeaponSet w) {
		curWeapon = w;
	}

	/**
	 * Adds a weapon to the player's inventory.
	 * 
	 * @param w		The weapon to be added to the inventory.
	 */
	public void addWeapon(Weapon w) {
		inventory.add(w);
	}


	public void update() {
		//****If Player is to extend MyDrawable, it will need to implement this method.
		//The Player class should not need to be updated
	}

	/**
	 * Method handling a single tap event on the playing field. 
	 * Tells the weapon to attack using its single tap attack
	 * at the given (x,y) location.
	 * 
	 * @param x		The x coordinate of the tap (abstract units.)
	 * @param y		The y coordinate of the tap. (abstract units.)
	 */
	public Projectile basicAttack(double x, double y) {
		return curWeapon.attack(x, y);
	}

	/**
	 * Method handling a double tap event on the playing field. 
	 * Tells the weapon to attack using its double tap attack
	 * at the given (x,y) location.
	 * 
	 * @param x		The x coordinate of the tap (abstract units.)
	 * @param y		The y coordinate of the tap. (abstract units.)
	 */
	public void OnDoubleTap(double x, double y) {

	}

	/**
	 * Method handling a long press event on the playing field. 
	 * Tells the weapon to attack using its long press attack
	 * at the given (x,y) location.
	 * 
	 * @param x		The x coordinate of the tap (abstract units.)
	 * @param y		The y coordinate of the tap. (abstract units.)
	 */
	public void OnLongPress(double x, double y) {

	}

	/**
	 * Method handling a fling event on the playing field. 
	 * Tells the weapon to attack using its fling attack.
	 */
	public void OnFling(/*don't know parameters*/) {

	}

	//We can implement this later when we have different types of items
	public boolean buy(Item item) {

		/*if(!checkCost(item.cost))
				return false;*/

		if(credits < item.cost)
			return false;
		credits -= item.cost;

		//Do something with the item!!!

		return true;
	}
	public boolean destroy() {

		if(healthPoints <= 0)
			return true;
		return false;
	}
	
	public void draw(Canvas c) {
		GraphicsEngine.draw(c, Castle.castle, 0, top);
		GraphicsEngine.drawHealthBar(c, PE.screenPercentX(0), PE.screenPercentY(0), PE.screenPercentX(100), PE.screenPercentY(2.5), healthPoints, baseHealth);
		curWeapon.draw(c);
	}
	public void update(long milli) {
		for(Weapon w : inventory) {
			w.update(milli);
		}
		curWeapon.update(milli);
	}
	public int height() {
		return 10;
	}
}
